package com.ganxiang.ximenhong.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;


/**
 * @author sam
 */
public class RoundBgUtil {
	public static int bgRadius = 4;
	
	/**
	 * 
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getBgStateDrawable(Context context, int parseColor) {
		StateListDrawable sd = new StateListDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = { radius, radius, radius, radius, radius, radius, radius, radius};

		GradientDrawable normal = new GradientDrawable();// 创建drawable
		normal.setColor(parseColor);// 内部填充颜色
		normal.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	
	/**
	 * 上方左右兩角
	 * @param context
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getBgTopStateDrawable(Context context, int parseColor) {
		StateListDrawable sd = new StateListDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = {radius, radius, radius, radius, 0, 0, 0, 0 };

		GradientDrawable normal = new GradientDrawable();// 创建drawable
		normal.setColor(parseColor);// 内部填充颜色
		normal.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	/**
	 * 底部左右兩角
	 * @param context
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getBgBottomStateDrawable(Context context, int parseColor) {
		StateListDrawable sd = new StateListDrawable();
        int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = {0, 0, 0, 0, radius, radius, radius, radius};

		GradientDrawable normal = new GradientDrawable();// 创建drawable
		normal.setColor(parseColor);// 内部填充颜色
		normal.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	/**
	 * 左下角
	 * @param context
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getLeftButtonDrawable(Context context, int nomalColor, int pressColor) {
		StateListDrawable sd = new StateListDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = { 0, 0, 0, 0, 0, 0 , radius, radius};

		GradientDrawable normal = new GradientDrawable();
		normal.setColor(nomalColor);
		normal.setCornerRadii(rad);

		GradientDrawable pressed = new GradientDrawable();
		pressed.setColor(pressColor);
		pressed.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_pressed }, pressed);
		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	/**
	 * 右下角
	 * @param context
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getRightButtonDrawable(Context context, int nomalColor, int pressColor) {
		StateListDrawable sd = new StateListDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = { 0, 0, 0, 0, radius, radius, 0, 0};

		GradientDrawable normal = new GradientDrawable();
		normal.setColor(nomalColor);
		normal.setCornerRadii(rad);

		GradientDrawable pressed = new GradientDrawable();
		pressed.setColor(pressColor);
		pressed.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_pressed }, pressed);
		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	
	/**
	 * 左下 右下角
	 * @param context
	 * @param nomalColor
	 * @param pressColor
	 * @return
	 */
	public static StateListDrawable getBottomButtonDrawable(Context context, int nomalColor, int pressColor) {
		StateListDrawable sd = new StateListDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = { 0, 0, 0, 0, radius, radius, radius, radius};

		GradientDrawable normal = new GradientDrawable();
		normal.setColor(nomalColor);
		normal.setCornerRadii(rad);

		GradientDrawable pressed = new GradientDrawable();
		pressed.setColor(pressColor);
		pressed.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_pressed }, pressed);
		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	
	/**
	 * 
	 * @param radius 4个角的度数
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getBgStateDrawable(Context context, int parseColor, int bgRadius) {
		StateListDrawable sd = new StateListDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = { radius, radius, radius, radius, radius, radius, radius, radius};

		GradientDrawable normal = new GradientDrawable();// 创建drawable
		normal.setColor(parseColor);// 内部填充颜色
		normal.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_enabled }, normal);


		return sd;
	}
	
	/**
	 * 圓角背景
	 * @param context
	 * @param parseColor 背景颜色 "FFFFFF"
	 * @return
	 */
	public static StateListDrawable getBgRoundDrawable(Context context, int radius, int parseColor) {
		StateListDrawable sd = new StateListDrawable();
        int r = Utils.dipToPixels(context, radius);
		float rad[] = {r, r, r, r, r, r, r, r};

		GradientDrawable normal = new GradientDrawable();// 创建drawable
		normal.setColor(parseColor);// 内部填充颜色
		normal.setCornerRadii(rad);

		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	
	public static StateListDrawable getClickDrawable(Context context,int nomalColor, int parseColor) {
		StateListDrawable sd = new StateListDrawable();

		GradientDrawable normal = new GradientDrawable();
		normal.setColor(nomalColor);

		GradientDrawable pressed = new GradientDrawable();
		pressed.setColor(parseColor);
		sd.addState(new int[] { android.R.attr.state_pressed }, pressed);
		sd.addState(new int[] { android.R.attr.state_enabled }, normal);

		return sd;
	}
	
	public static StateListDrawable getStrokeDrawable(Context context,int nomalColor, int strokeColor ,int strokeWidth) {
		StateListDrawable sd = new StateListDrawable();
		GradientDrawable normal = new GradientDrawable();
		int radius = Utils.dipToPixels(context, bgRadius);
		float rad[] = { radius, radius, radius, radius, radius, radius, radius, radius};
		normal.setColor(nomalColor);
		normal.setStroke(strokeWidth, strokeColor);
		normal.setCornerRadii(rad);
		sd.addState(new int[] { android.R.attr.state_enabled }, normal);
		return sd;
	}
	
	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  
	    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
	            bitmap.getHeight(), Config.ARGB_8888);  
	    Canvas canvas = new Canvas(output);  
	    final int color = 0xff424242;  
	    final Paint paint = new Paint();  
	    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
	    final RectF rectF = new RectF(rect);  
	    final float roundPx = pixels;  
	    paint.setAntiAlias(true);  
	    canvas.drawARGB(0, 0, 0, 0);  
	    paint.setColor(color);  
	    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
	    canvas.drawBitmap(bitmap, rect, rect, paint);  
	    return output;  
	}  
}
